package cn.itcast.bos.web.action.system;

import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 *
 * @author: SOS
 * @create: 2018-03-09 15:50
 */

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {

    @Autowired
    private UserService userService;


    @Action(value = "user_login", results = {@Result(name = SUCCESS, type = "redirect", location = "./index.html"),
            @Result(name = LOGIN, type = "redirect", location = "./login.html")})
    public String userLogin() {

        //y用户名密码都保存在model中
        //基于shiro实现登录
        Subject subject = SecurityUtils.getSubject();

        //创建usernamepasswordtoken
        AuthenticationToken authenticationToken = new UsernamePasswordToken(model.getUsername(), model.getPassword());

        //登录,如果没有异常就登录成功
        try {
            subject.login(authenticationToken);
            //调用shiro的执行流程 application--->subject--->securityManager--->realm安全数据
            //需要将配置文件修改定义realm,并注入到安全管理器中
            return SUCCESS;
        } catch (AuthenticationException e) {
            //登录失败,抛出异常信息
            e.printStackTrace();
            return LOGIN;
        }
    }

    /**
     * 用户的退出功能
     *
     * @return
     */
    @Action(value = "user_logout", results = {@Result(name = SUCCESS, type = "redirect", location = "./login.html"),
            @Result(name = "input", location = "./index.html", type = "redirect")})
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        System.out.println("退出成功...");
        return SUCCESS;
    }

    /**
     * 用户的列表查询
     * @return
     */
    @Action(value = "user_list", results = {@Result(name = SUCCESS, type = "json"),
            @Result(name = "input", location = "./pages/system/userlist.html", type = "redirect")})
    public String userList() {
        List<User> users = userService.findAll();
        ActionContext.getContext().getValueStack().push(users);
        return SUCCESS;
    }

    private String[] roleIds;

    public void setUserIds(String[] roleIds) {
        this.roleIds = roleIds;
    }

    @Action(value = "user_save", results = {@Result(name = SUCCESS, location = "./pages/system/userlist.html", type = "redirect"),
            @Result(name = "input", location = "./pages/system/userlist.html", type = "redirect")})
    public String userSave() {
        userService.save(model,roleIds);

        return SUCCESS;
    }

}
